<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>受控组件</title>

    <style>
        .weatcher{
            font-size: 32px;
            font-weight: bold;
        }
    </style>
</head>
<body>
    <div id="test"></div>
    <script src="./js/react.development.js" crossorigin></script> 
    <script src="./js/react-dom.development.js" crossorigin></script>
    <script src="./js/babel.min.js"></script>
    <script type="text/babel"> /* 此处一定要写babel */
        class Login extends React.Component{
            state = {username:'',password: ''}
            
            handleSubmit = (event) => {
                event.preventDefault()
                const { username, password } = this.state
                alert(`您输入的用户名是: ${username},您输入的密码是: ${password}`)
            }

            // 保存表单数据到状态中
            saveFormData = (dataType,value) => {
                this.setState({
                    [dataType]: value
                })
            }
           render(){
            return (
                <form onSubmit={this.handleSubmit}>
                    用户名：<input type="text" onChange={(event) => this.saveFormData('username',event.target.value)} name="username" /> &nbsp;
                    密码：<input type="password" onChange={(event) => this.saveFormData('password',event.target.value)} name="password" />
                    <button>登录</button>
                </form>
            )
           }
        }

        ReactDOM.render(<Login />,document.getElementById('test'))
    </script>
</body>
</html>